﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Log;
using UtilZ.Dotnet.Ex.DataStruct;

namespace ConAppCore.Test
{
    internal class TestAsynQueueZ
    {
        private static AsynQueueZ<AsynQueueZItem> _asynQueueZ = null;
        public static void Test()
        {
            var option = new AsynQueueOptions()
            {
                FullMode = ContainerFullMode.DropOld,
                //Capcity = 10000,
            };
            _asynQueueZ = new AsynQueueZ<AsynQueueZItem>(Callback, option);
            _asynQueueZ.Start();


            Task.Factory.StartNew(Producer);
        }

        private static void Producer()
        {
            long id = 0;

            while (id < long.MaxValue)
            {
                _asynQueueZ.Enqueue(new AsynQueueZItem(id++, new byte[6000]));

                if (id % 10000 == 0)
                {
                    Loger.Info($"生产:{id},剩余消费:{_asynQueueZ.Count}");
                }

                Thread.SpinWait(100);
            }
        }


        private static void Callback(AsynQueueZItem item, object obj)
        {
            Thread.SpinWait(1000);
        }
    }

    internal class AsynQueueZItem
    {
        public long Id { get; set; }

        public byte[] Bytes { get; set; }


        public AsynQueueZItem(long id, byte[] buffer)
        {
            this.Id = id;
            this.Bytes = buffer;
        }
    }
}
